<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper Session 管理与超时机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .pulse-icon {
            animation: pulse 2s infinite;
        }
        
        @keyframes pulse {
            0% {
                transform: scale(1);
                opacity: 1;
            }
            50% {
                transform: scale(1.1);
                opacity: 0.8;
            }
            100% {
                transform: scale(1);
                opacity: 1;
            }
        }
        
        .first-letter {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h1 class="text-5xl font-bold mb-4 tracking-tight">
                    <i class="fas fa-network-wired mr-4 pulse-icon"></i>
                    Zookeeper Session 机制
                </h1>
                <p class="text-xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    深入理解分布式协调服务的核心：Session 管理与超时机制
                </p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">在</span>Zookeeper 中，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded font-mono">Session</code> 是一个重要的概念，它用于管理客户端与 Zookeeper 服务的连接状态。Session 的管理和超时机制确保了客户端与 Zookeeper 之间的通信能够在网络不稳定或客户端出现故障时正确地处理。
            </p>
        </div>

        <!-- Session Lifecycle Visualization -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 text-center">
                <i class="fas fa-chart-line mr-3 text-purple-600"></i>
                Session 生命周期
            </h2>
            <div class="mermaid">
                graph LR
                    A[客户端连接] -->|创建| B[Session 建立]
                    B -->|心跳维持| C[Session 活跃]
                    C -->|正常断开| D[Session 关闭]
                    C -->|超时| E[Session 失效]
                    E -->|清理| F[临时节点删除]
                    C -->|网络恢复| C
                    E -->|重连| A
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4CAF50,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f44336,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Session Management Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <h3 class="text-2xl font-bold mb-6 section-title text-gray-800">
                    <i class="fas fa-cogs mr-3 text-purple-600"></i>
                    Session 管理
                </h3>
                
                <div class="space-y-6">
                    <div>
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">
                            <i class="fas fa-plug mr-2 text-blue-500"></i>
                            Session 的创建
                        </h4>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">建立连接：</strong>
                                    <span class="text-gray-600">当客户端首次连接到 Zookeeper 集群时，Zookeeper 会为该客户端创建一个新的 Session，并分配唯一的 Session ID。</span>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">Session 相关数据：</strong>
                                    <span class="text-gray-600">Zookeeper 会为客户端分配会话状态和事务日志等资源，客户端可在 Session 存活期间进行读写操作。</span>
                                </div>
                            </li>
                        </ul>
                    </div>
                    
                    <div>
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">
                            <i class="fas fa-heartbeat mr-2 text-red-500"></i>
                            Session 的维护
                        </h4>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">心跳机制：</strong>
                                    <span class="text-gray-600">客户端定期向服务器发送心跳信号，告知服务器客户端仍然在线。</span>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">Session 数据：</strong>
                                    <span class="text-gray-600">每个 Session 可绑定临时节点和会话数据，通过 Watcher 机制进行监听。</span>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <h3 class="text-2xl font-bold mb-6 section-title text-gray-800">
                    <i class="fas fa-clock mr-3 text-purple-600"></i>
                    Session 超时机制
                </h3>
                
                <div class="space-y-6">
                    <div>
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">
                            <i class="fas fa-hourglass-half mr-2 text-orange-500"></i>
                            超时定义
                        </h4>
                        <p class="text-gray-600">
                            Session 超时是指客户端与服务器在规定时间内未能正常通信时，Zookeeper 将该 Session 标记为超时。超时时间可通过客户端配置设置。
                        </p>
                    </div>
                    
                    <div>
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">
                            <i class="fas fa-sync-alt mr-2 text-blue-500"></i>
                            工作流程
                        </h4>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-triangle text-yellow-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">心跳丢失：</strong>
                                    <span class="text-gray-600">服务器在超时时间内未收到心跳信号。</span>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">Session 失效：</strong>
                                    <span class="text-gray-600">Session 被标记为失效，临时节点被删除。</span>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-bell text-purple-500 mt-1 mr-3"></i>
                                <div>
                                    <strong class="text-gray-700">Watchers 触发：</strong>
                                    <span class="text-gray-600">触发相关 Watchers，通知其他客户端。</span>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- Session Recovery -->
        <div class="highlight-box mb-12">
            <h3 class="text-xl font-bold mb-4 text-gray-800">
                <i class="fas fa-redo mr-3 text-purple-600"></i>
                Session 恢复机制
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div>
                    <h4 class="font-semibold text-gray-700 mb-2">
                        <i class="fas fa-link mr-2 text-green-500"></i>
                        重新连接
                    </h4>
                    <p class="text-gray-600">
                        客户端在 Session 超时前重新连接将创建新的 Session，需要重新建立会话状态并注册 Watchers。
                    </p>
                </div>
                <div>
                    <h4 class="font-semibold text-gray-700 mb-2">
                        <i class="fas fa-database mr-2 text-blue-500"></i>
                        恢复操作
                    </h4>
                    <p class="text-gray-600">
                        通过新 Session 获取之前的状态和数据，重新创建所需的临时节点。
                    </p>
                </div>
            </div>
        </div>

        <!-- Application Scenarios -->
        <div class="bg-gradient-to-r from-purple-50 to-blue-50 rounded-2xl p-8 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 text-center">
                <i class="fas fa-lightbulb mr-3 text-yellow-500"></i>
                Session 应用场景
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-lg p-6 shadow-md">
                    <h4 class="font-bold text-lg mb-3 text-purple-700">
                        <i class="fas fa-folder-open mr-2"></i>
                        临时节点管理
                    </h4>
                    <p class="text-gray-600">
                        临时节点与 Session 紧密相关，当 Session 失效时，所有关联的临时节点会被自动删除，保证了节点的自动清理和资源的有效管理。
                    </p>
                </div>
                <div class="bg-white rounded-lg p-6 shadow-md">
                    <h4 class="font-bold text-lg mb-3 text-blue-700">
                        <i class="fas fa-eye mr-2"></i>